<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>socket.io 示例</title>
  </head>
  <body>
    <button>发送消息</button>
    <script src="https://cdn.socket.io/4.4.1/socket.io.min.js"></script>
    <script>
      // 建立连接
      // const socket = io('http://192.168.36.198:3000', {
      const socket = io('http://localhost:3000', {
        transports: ['websocket', 'polling'],
      })
      // 监听连接建立状态
      socket.on('connect', () => {
        console.log('建立连接...')
      })
      // 监听连接断开状态
      socket.on('disconnect', () => {
        console.log('断开连接...')
      })
      // 点击发消息
      document.querySelector('button').addEventListener('click', () => {
        socket.emit('xxx', 'helloworld')
      })
    </script>
  </body>
</html>
